package PlaneGame.CODE;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import static PlaneGame.CODE.GameFrame.*;

public abstract class GameImage extends GameObject{
    int width,height;
    int imageNumber;
    int drawPos;
    String[] paths;
    BufferedImage[] images;
    int imageRate,imageRateCount;

    public GameImage(int width, int height, int x,int y,int imageNumber, String[] paths) {
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
        this.imageNumber = imageNumber;
        this.paths = paths;
        images=new BufferedImage[imageNumber];
        if (paths.length > 0) {
            for (int i = 0; i < images.length; i++) {
                try {
                    images[i] = ImageIO.read(new File(paths[i]));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        } else {
            System.out.println("还没输入路径");
        }
    }

    public GameImage() {
        init();
    }

    @Override
    void init() {
        super.init();
        suspendFlag=false;
        changeRate=50;
        drawPos=0;
        imageRate=4;
        imageRateCount=0;
    }
    @Override
    void draw(Graphics g) {
        g.drawImage(images[drawPos],x,y,width,height,null);
    }

    @Override
    void change() {
        nextDrawPos();
    }
    void nextDrawPos(){//轮播图片
        if(imageNumber==1){//如果是单图，则不用轮播
            return;
        }
        if(imageRateCount<imageRate){//每几轮循环切换一次,防止切换速度过快
            imageRateCount++;
            return;
        }
        imageRateCount=0;
        if(drawPos<imageNumber-1){//切换下一张
            drawPos++;
        }else {//若播到最后一张则切回第一张
            drawPos=0;
        }
    }
    boolean contain(int x, int y){
        return (x > this.x
                && x < (this.x + this.width)
                && y > this.y
                && y < (this.y + this.height));
    }
}
